<?php

//设置时区，后面可以让用户选择，如果只面向中国就算了
if(function_exists('date_default_timezone_set')) date_default_timezone_set("Asia/Shanghai");

if(PHP_VERSION < '5.3.0') define('__DIR__', dirname(__FILE__));

define('ROOT_DIR', dirname(__DIR__)."/");
include_once ROOT_DIR.'/lib/comm.func.php';
include_once ROOT_DIR.'/lib/image.func.php';

//加载配置
if(file_exists(ROOT_DIR.'/conf/conf.php')) {
	require_once ROOT_DIR.'/conf/conf.php';
} else {
	;
}

//启用session
session_start(); 
session_set_cookie_params($cf_sessionout);//设置session失效时间

include_once ROOT_DIR."/lib/db.mysql.class.php";
$_db = new cmysql($cf_dbhost, $cf_dbuser, $cf_dbpasswd, $cf_dbname);//加载数据库连接

//获取os名、路径分隔符、换行符
if(substr(PHP_OS, 0, 3) == 'WIN' || (isset($_ENV['OS']) && strstr($_ENV['OS'], 'indow'))) {
	$_os = 'windows';
	$_separator = '\\';
	$_lr = "\r\n";
} else {
	$_os = 'linux';
	$_separator = '/';
	$_lr = "\n";
}

//获得当前访问url,带query参数
if(isset($_SERVER['HTTP_HOST'])) {
	$_host = $_SERVER['SERVER_NAME'];
	if(isset($_SERVER['REQUEST_URI'])) $_requrl = $_SERVER['REQUEST_URI'];
	if(!isset($_requrl) && isset($_SERVER['HTTP_X_REWRITE_URL'])) $_requrl = $_SERVER['HTTP_X_REWRITE_URL'];
	if(!isset($_requrl)) {
		$_requrl = $_SERVER['SCRIPT_NAME'];
		if(!empty($_SERVER['QUERY_STRING'])) $_requrl .= '?'.$_SERVER['QUERY_STRING'];
	}
	
	if(false!==strpos($_requrl ,$cf_entry)){//如果访问是主要页面, 获取访问的根地址.
		$pos = strpos($_requrl ,$cf_entry);
		$qpos = strpos($_requrl ,"?");
		if(false!==$qpos)
			$len = $qpos;
		else 
			$len = strlen($_requrl);
		if($pos+strlen($cf_entry)==$len){
			$_serverroot = substr($_requrl, 0, $pos);
		}
	}
	else{
		$_serverroot = "/";
	}
	define('SERVER_ROOT', $_serverroot);
}

//获取客户端ip
$_clientip = '127.0.0.1';
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	$_clientip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	$_clientip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	$_clientip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	$_clientip = $_SERVER['REMOTE_ADDR'];
}
$_clientip = preg_replace('/^([\d\.]+).*/', "\\1", $_clientip);

//处理GPC等参数, 并将GET的参数全部设置到POST中去
//unset($HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SERVER_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $_REQUEST);
$magic_quotes_gpc = get_magic_quotes_gpc();
if($magic_quotes_gpc) {
	$_POST = unaddslashes($_POST);
	$_GET = unaddslashes($_GET);
	$_COOKIE = unaddslashes($_COOKIE);
}
//将GET参数放入POST中
foreach($_GET as $key=>$val){
	if(array_key_exists($key, $_POST)){
		if(is_array($_POST[$key])) $_POST[$key][] = $val;
		else {
			$_POST[$key] = array($_POST[$key], $val);
		}
	}
	else{
		$_POST[$key] = $val;
	}
}

?>